package com.rabbiter.oes.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbiter.oes.entity.Student;
import com.rabbiter.oes.mapper.StudentMapper;
import com.rabbiter.oes.service.StudentService;
import com.rabbiter.oes.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;


    @Override
    public IPage<Student> findAll(
            Page<Student> page, String name, String grade,
            String tel, String institute, String major, String clazz) {
        name = ("@".equals(name) ? "" : name);
        grade = ("@".equals(grade) ? "" : grade);
        tel = ("@".equals(tel) ? "" : tel);
        institute = ("@".equals(institute) ? "" : institute);
        major = ("@".equals(major) ? "" : major);
        clazz = ("@".equals(clazz) ? "" : clazz);
        return studentMapper.findAll(page, name, grade, tel, institute, major, clazz);
    }

    @Override
    public Student findById(Integer studentId) {
        return studentMapper.findById(studentId);
    }

    @Override
    public int deleteById(Integer studentId) {
        return studentMapper.deleteById(studentId);
    }

    @Override
    public int update(Student student) {
        return studentMapper.update(student);
    }

    @Override
    public int updatePwd(Student student) {
        return studentMapper.updatePwd(student);
    }

    @Override
    public int add(Student student) {
        return studentMapper.add(student);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object batchImportStudents(List<Student> students) {
        int successCount = 0;
        int failCount = 0;

        for (Student student : students) {
            try {
                // 必填字段校验
                if (student.getStudentId() == null ||
                        student.getStudentName() == null || student.getStudentName().isEmpty() ||
                        student.getTel() == null || student.getTel().isEmpty() ||
                        student.getCardId() == null || student.getCardId().isEmpty() ||
                        student.getInstitute() == null || student.getInstitute().isEmpty()) {
                    failCount++;
                    continue;
                }

                // 检查警号或身份证是否已存在
                int count = studentMapper.existsByStudentIdOrCardId(student.getStudentId(), student.getCardId());
                if (count > 0) {
                    failCount++;
                    continue;
                }

                // 设置默认值
                if (student.getPwd() == null || student.getPwd().isEmpty()) {
                    student.setPwd("123456");
                }
                if (student.getRole() == null || student.getRole().isEmpty()) {
                    student.setRole("2"); // 字符串
                }
                if (student.getGrade() == null || student.getGrade().isEmpty()) {
                    student.setGrade(String.valueOf(java.time.Year.now().getValue()));
                }

                // 执行插入
                int result = studentMapper.add(student);
                if (result > 0) {
                    successCount++;
                } else {
                    failCount++;
                }

            } catch (Exception e) {
                failCount++;
                e.printStackTrace(); // 建议使用日志框架
            }
        }
        Map<String, Object> result = new HashMap<>();
        result.put("successCount", successCount);
        result.put("failCount", failCount);

        // 使用你已有的 ApiResultHandler 返回
        return ApiResultHandler.buildApiResult(200, "批量导入完成", result);
    }


}
